package com.huarun.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class URLUtil
{
  public static String encodeURIComponent(String component)
  {
    String result = null;
    try
    {
      result = URLEncoder.encode(component, "UTF-8")
        .replaceAll("\\%28", "(")
        .replaceAll("\\%29", ")")
        .replaceAll("\\+", "%20")
        .replaceAll("\\%27", "'")
        .replaceAll("\\%21", "!")
        .replaceAll("\\%7E", "~");
    }
    catch (UnsupportedEncodingException e) {
      result = component;
    }

    return result;
  }

  public static Map<String, String> splitQuery(String queryString)
  {
    Map query_pairs = new ConcurrentHashMap();
    String[] pairs = queryString.split("&");
    for (String pair : pairs) {
      int idx = pair.indexOf("=");
      try
      {
        String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
        String value = (idx > 0) && (pair.length() > idx + 1) ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;

        if (!key.isEmpty())
          query_pairs.put(key, value);
      }
      catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
    }
    return query_pairs;
  }

  public static String replaceLanToWan(String lanUrl, String wanHost)
  {
    String pattern = "http://\\d*\\.\\d*\\.\\d*\\.\\d*:\\d*";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(lanUrl);
    if (m.find()) {
      String host = m.group(0);
      return lanUrl.replace(host, wanHost);
    }
    return lanUrl;
  }
}

/* Location:           D:\msg\反编译\Webuilding-0.0.1-SNAPSHOT(5)\BOOT-INF\classes\
 * Qualified Name:     com.huarun.common.util.URLUtil
 * JD-Core Version:    0.6.2
 */